﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SEVN.Web.Framework;

namespace SEVN.Web.Framework.Controls
{
    public partial class TitleControl : System.Web.UI.UserControl
    {

        private ControlOnPage _applyToControlOnPage;

        public ControlOnPage ApplyToControlOnPage
        {
            get { return _applyToControlOnPage; }
            set { _applyToControlOnPage = value; }
        }

        private MyPage _pageContainer;

        public MyPage PageContainer
        {
            get { return _pageContainer; }
            set { _pageContainer = value; }
        }

        private Skin _skinContainer;

        public Skin SkinContainer
        {
            get { return _skinContainer; }
            set { _skinContainer = value; }
        }

        private char _position;

        public char Position
        {
            get { return _position; }
            set { _position = value; }
        }

        private int _maxOrder;
        public int MaxOrder
        {
            get { return _maxOrder; }
            set { _maxOrder = value; }
        }

        private int _minOrder;

        public int MinOrder
        {
            get { return _minOrder; }
            set { _minOrder = value; }
        }      

        protected void Page_Load(object sender, EventArgs e)
        {

            if (_applyToControlOnPage.Position == 'L')
            {
                ibtLeft.Visible = false;
                ibtRight.Visible = true;
            }
            else
                if (_applyToControlOnPage.Position == 'R')
                {
                    ibtLeft.Visible = true;
                    ibtRight.Visible = false;
                }
                else
                {
                    ibtLeft.Visible = false;
                    ibtRight.Visible = false;
                }

            //if (_skinContainer == null)
            //{
                if (_applyToControlOnPage.Order == _maxOrder)
                {                    
                    ibtDown.Visible = false;
                    if(_maxOrder==_minOrder)
                        ibtUp.Visible = false;
                    else
                        ibtUp.Visible = true;
                }
                else if (_applyToControlOnPage.Order == _minOrder)
                {
                    ibtUp.Visible = false;
                    if (_minOrder == _maxOrder)
                        ibtDown.Visible = false;
                    else
                        ibtDown.Visible = true;
                }
                else
                {
                    ibtUp.Visible = true;
                    ibtDown.Visible = true;
                }
            //}

                //ibtDown.Attributes.Add("onclick", " this.disabled = true; " + Page.ClientScript.GetPostBackEventReference(ibtDown, null) + ";");
                //ibtUp.Attributes.Add("onclick", " this.disabled = true; " + Page.ClientScript.GetPostBackEventReference(ibtUp, null) + ";");


        }
       



        protected void ibtRight_Click(object sender, ImageClickEventArgs e)
        {
            if (_skinContainer == null)
            {
                Ultility.MoveControlToRight(_pageContainer, _applyToControlOnPage);
                if (Request.Url != null)
                    Response.Redirect(Request.Url.ToString());
            }
            else
            {
                Ultility.MoveControlToRight(_skinContainer, _applyToControlOnPage);
                if (Request.Url != null)
                    Response.Redirect(Request.Url.ToString());
            }
        }

        protected void ibtLeft_Click(object sender, ImageClickEventArgs e)
        {
            if (_skinContainer == null)
            {
                Ultility.MoveControlToLeft(_pageContainer, _applyToControlOnPage);
                if (Request.Url != null)
                    Response.Redirect(Request.Url.ToString());
            }
            else
            {
                Ultility.MoveControlToLeft(_skinContainer, _applyToControlOnPage);
                if (Request.Url != null)
                    Response.Redirect(Request.Url.ToString());
            }
        }

        protected void ibtUp_Click(object sender, ImageClickEventArgs e)
        {
            if (_skinContainer == null)
            {
                Ultility.UpOrderControl(_pageContainer, _applyToControlOnPage, _position);
               
                //if (Request.UrlReferrer != null)
                //    Response.Redirect(Request.UrlReferrer.ToString());
            }
            else
            {
                Ultility.UpOrderControl(_skinContainer, _applyToControlOnPage, _position);
                //if (Request.UrlReferrer != null)
                //    Response.Redirect(Request.UrlReferrer.ToString());
            }
        }

        protected void ibtDown_Click(object sender, ImageClickEventArgs e)
        {
            if (_skinContainer == null)
            {
                Ultility.DownOrderControl(_pageContainer, _applyToControlOnPage, _position);
                //if (Request.UrlReferrer != null)
                //    Response.Redirect(Request.UrlReferrer.ToString());
            }
            else
            {
                Ultility.DownOrderControl(_skinContainer, _applyToControlOnPage, _position);
                //if (Request.UrlReferrer != null)
                //    Response.Redirect(Request.UrlReferrer.ToString());
            }
        }
    }
}